let http = require('http')
let querystring = require('querystring')
let url = require('url')

let server = http.createServer((req,res)=>{
  let {pathname,query} = url.parse(req.url,true)
  console.log(pathname);
  let method = req.method.toLowerCase();
  console.log(method);
  let headers = req.headers;
  console.log(headers);

  let arr=[];
  req.on('data',(chunk)=>{
    arr.push(chunk);
  });
  req.on('end',()=>{
    let str = Buffer.concat(arr).toString();
    if (req.headers['content-type']==='application/x-www-form-urlencoded'){
      let obj = querystring.parse(str);
      res.setHeader('content-type','application/json;charset=utf8')
      res.end(JSON.stringify(obj));
    }
  })
});

server.listen(3000);
